/**
 * @description router全局配置，如有必要可分文件抽离，其中asyncRoutes只有在intelligence模式下才会用到，pro版只支持remixIcon图标，具体配置请查看vip群文档
 */
import type { App } from 'vue'
import type { RouteRecordName, RouteRecordRaw } from 'vue-router'
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import type { VabRouteRecord } from './types.ts'
import { base, isHashRouterMode } from '/@/config'
import { setupPermissions } from '/@/router/permissions'
// 引入模块路由
import examineRoutes from './modules/examineRoutes'
import meetingManagementRoutes from './modules/meetingManagementRoutes'
import Layout from '/@vab/layouts/index.vue'

export const constantRoutes: VabRouteRecord[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('/@/views/login/Login.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('/@/views/login/Register.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/password',
    name: 'Password',
    component: () => import('/@/views/login/Password.vue'),
    meta: {
      hidden: true,
    },
  },
  // {
  //   path: '/redirect',
  //   name: 'Redirect',
  //   component: () => import('/@/views/redirect/Redirect.vue'),
  //   meta: {
  //     hidden: true,
  //   },
  // },
  {
    path: '/403',
    name: '403',
    component: () => import('/@/views/error/403.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/404',
    name: '404',
    component: () => import('/@/views/error/404.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/',
    name: 'Root',
    meta: {
      title: '会议列表',
      hidden: true,
      icon: 'slideshow-line',
    },
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('../views/home/information/index.vue'),
        meta: {
          title: '会议列表',
          noColumn: true,
          icon: 'slideshow-line',
          noClosable: true,
          noKeepAlive: true,
        },
      },
    ],
  },
  {
    path: '/dataScreen',
    name: 'DataScreen',
    meta: {
      title: '数据查看',
      hidden: true,
      icon: 'slideshow-line',
    },
    children: [
      {
        path: 'index',
        name: 'DataScreenIndex',
        component: () => import('../views/dataScreen/index.vue'),
        meta: {
          title: '数据查看',
          noColumn: true,
          icon: 'slideshow-line',
          noClosable: true,
          noKeepAlive: true,
        },
      },
    ],
  },
  {
    path: '/signIn',
    name: 'SignIn',
    meta: {
      title: '签到',
      hidden: true,
      icon: 'slideshow-line',
    },
    children: [
      {
        path: 'signInPage',
        name: 'SignInPage',
        component: () => import('../views/signIn/index.vue'),
        meta: {
          title: '签到',
          noColumn: true,
          icon: 'slideshow-line',
          noClosable: true,
          noKeepAlive: true,
        },
      },
    ],
  },
  {
    path: '/meeting',
    name: 'MeetingRoomsManage',
    meta: {
      hidden: true,
      title: '会议室管理',
      icon: 'building-line',
    },
    children: [
      {
        path: 'meetingRooms',
        name: 'MeetingRooms',
        component: () => import('../views/home/meetingRooms/index.vue'),
        meta: {
          title: '会议室管理(旧版)',
          icon: 'building-line',
          hidden: true,
        },
      },
      {
        path: 'meetingRooms-new',
        name: 'MeetingRoomsNew',
        component: () => import('../views/home/meetingRooms-new/index.vue'),
        meta: {
          title: '会议室管理',
          icon: 'building-4-line',
        },
      },
      {
        path: 'meetingRooms-overview',
        name: 'MeetingRoomsOverview',
        component: () => import('../views/home/meetingRooms-overview/index.vue'),
        meta: {
          title: '会议室概览',
          icon: 'dashboard-line',
        },
      },
      {
        path: 'reserve',
        name: 'Reserve',
        component: () => import('/@/views/reserve/index.vue'),
        meta: {
          title: '预约审批',
          icon: 'list-unordered',
        },
        // 用户提交预约，管理员课查看和审批
        // 预约时可看会议室的议程安排
      },
      {
        path: 'reserve-new',
        name: 'ReserveNew',
        component: () => import('/@/views/reserve-new/index.vue'),
        meta: {
          title: '预约管理（新）',
          icon: 'calendar-todo-line',
        },
      },
    ],
  },
  {
    path: '/setting',
    name: 'Setting',
    meta: {
      title: '系统配置',
      hidden: true,
      icon: 'home-8-line',
    },
    children: [
      {
        path: 'setList',
        name: 'SetList',
        component: () => import('/@/views/home/setting/index.vue'),
        meta: {
          title: '系统配置',
          icon: 'home-line',
        },
      },
    ],
  },
  {
    path: '/user',
    name: 'User',
    meta: {
      title: '用户管理',
      hidden: true,
      icon: 'home-8-line',
    },
    children: [
      {
        path: 'userList',
        name: 'UserList',
        component: () => import('/@/views/home/user/index.vue'),
        meta: {
          title: '用户管理',
          icon: 'home-line',
        },
      },
    ],
  },

  {
    path: '/place',
    name: 'Place',
    meta: {
      title: '住宿管理',
      hidden: true,
      icon: 'home-8-line',
    },
    children: [
      {
        path: 'list',
        name: 'PlaceList',
        component: () => import('../views/home/placeList/index.vue'),
        meta: {
          title: '住宿场所管理',
          icon: 'home-line',
        },
      },
    ],
  },
  {
    path: '/department',
    name: 'Department',
    component: () => import('/@/views/home/department/index.vue'),
    meta: {
      hidden: true,

      title: '部门管理',
      icon: 'office-building',
    },
  },
  {
    path: '/duty',
    name: 'Duty',
    component: () => import('/@/views/home/duty/index.vue'),
    meta: {
      title: '职务管理',
      hidden: true,

      icon: 'user-filled',
    },
  },
  ...meetingManagementRoutes,
  ...examineRoutes,
  {
    path: '/stayInfo',
    name: 'StayInfo',
    component: Layout,
    meta: {
      title: '住宿管理',
      icon: 'home-8-line',
    },
    children: [
      {
        path: 'index',
        name: 'StayInfoIndex',
        component: () => import('../views/home/stayInfo/index.vue'),
        meta: {
          title: '住宿管理',
          icon: 'user-line',
        },
      },
    ],
  },
]

export const asyncRoutes: VabRouteRecord[] = [
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    name: 'NotFound',
    meta: {
      title: '404',
      hidden: true,
    },
  },
]

const router = createRouter({
  history: isHashRouterMode ? createWebHashHistory(base) : createWebHistory(base),
  routes: constantRoutes as RouteRecordRaw[],
})

const fatteningRoutes = (routes: VabRouteRecord[]): VabRouteRecord[] => {
  return routes.flatMap((route) => {
    return route.children ? fatteningRoutes(route.children) : route
  })
}

const addRouter = (routes: VabRouteRecord[]) => {
  routes.forEach((route: VabRouteRecord) => {
    if (!router.hasRoute(route.name)) router.addRoute(route as RouteRecordRaw)
    if (route.children) addRouter(route.children)
  })
}

export const resetRouter = (routes: VabRouteRecord[] = constantRoutes) => {
  routes.map((route: VabRouteRecord) => {
    if (route.children) route.children = fatteningRoutes(route.children)
  })
  router.getRoutes().forEach((route) => {
    if (route.name) {
      const routeName: RouteRecordName = route.name
      router.hasRoute(routeName) && router.removeRoute(routeName)
    }
  })
  addRouter(routes)
}

export const setupRouter = (app: App<Element>) => {
  addRouter(asyncRoutes)
  setupPermissions(router)
  app.use(router)
  return router
}

export default router
